#!/usr/bin/env ruby
require 'rubygems'
require 'iconv'
require 'ferret'
require 'fileutils'
require 'ruby-debug'
require 'rmmseg'
require 'rmmseg/ferret'
require '../db_help/db_help'

include Ferret
include Ferret::Index
 
db_config = {"host" => '118.126.2.138', 
						'encoding' => 'utf8', 
						'username' => 'root', 
						'password' => 'admin', 
						'database' => 'tao_hidoudou'
						}

db_help = DBHelp::DBHelp.create(db_config)

RMMSeg::Dictionary.load_dictionaries
RMMSeg::Dictionary.load_words("dict/hidoudou.dic")
RMMSeg::Dictionary.load_words("dict/brands.dic")
RMMSeg::Dictionary.load_words("dict/clothes.dic")
RMMSeg::Dictionary.load_words("dict/star.dic")
 
analyzer = RMMSeg::Ferret::Analyzer.new { |tokenizer|
  Ferret::Analysis::LowerCaseFilter.new(tokenizer)
}

field_infos = FieldInfos.load(File.read("config/ferret.yml"))
index = Index.new(:path => "index/",
									:field_infos => field_infos,
									:create => true, 
									:analyzer => analyzer)
item_count = 0
max_doc_id = (db_help.query("select max_id from tao_counter where counter_id = 1 ").fetch_row()[0]).to_i
item_count = (db_help.query("select count(id) as item_count from tao_items where id > #{max_doc_id}").fetch_row()[0]).to_i
puts "--------------- start index [#{Time.now}] ------------------"
puts "item count : #{item_count}"

max_id = max_doc_id
0.upto((item_count / 1000 + 1)) do |i|
	items = db_help.query("select id, iid, nick, cat1_id, cat1_name, detail_url, title, format_title, props_str, price from tao_items where id > #{max_id} and status = 1 order by id limit 1000")
	
	items.each_hash do |item|
		max_id = item["id"].to_i
		puts "#{max_id}" if max_id % 100 == 0
		
		item_prop = {}
		#解释属性
		unless item["props_str"].nil?
			item_props = item["props_str"].split(";")
			
			item_props.each do |prop|
				props = prop.split(":")
				item_prop[props[0]] = props[1]
			end
		end

		#debugger
		#puts "#{item_prop["品牌"]}"
		doc = Document.new()
		doc[:id] = item["id"]
		doc[:iid] = item["iid"]
		doc[:nick] = item["nick"]
		doc[:title] = item["title"]
		doc[:cat1_id] = item["cat1_id"]
		doc[:cat1_name] = item["cat1_name"]
		doc[:brand] = item_prop["品牌"]
		doc[:format_title] = item["format_title"]	
		doc[:detail_url] = item["detail_url"]
		doc[:style_name] = item["props_str"]
		doc[:price] = item["price"]
		index << doc
		
		#db_help.query("update tao_items set indexed = 1 where id = " + item["id"])
		db_help.query("update tao_counter set max_id =  #{max_id} where counter_id = 1")
	end
end

index.optimize()                                
index.close()		
puts "--------------- end index [#{Time.now}] ------------------"

